import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_sign_record_entity.dart';
import 'package:flutter_app/r.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/CustomCalendar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'duty_cancel_dialog_logic.dart';

/**
 * @Description: 我的报名- 管理值班日期
 * @Author: 下雨了
 * @CreateDate: 2021-10-18 14:08
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-10-18 14:08
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class DutyCancelDialogPage extends StatefulWidget {
  final String title;

  final String collectId; // 征集表id

  const DutyCancelDialogPage({
    Key? key,
    required this.title,
    required this.collectId,
  }) : super(key: key);

  @override
  _DutyCancelDialogPageState createState() => _DutyCancelDialogPageState();
}

class _DutyCancelDialogPageState extends State<DutyCancelDialogPage> {
  final logic = Get.put(DutyCancelDialogLogic());
  final state = Get.find<DutyCancelDialogLogic>().state;
  final dialogState = Get.find<DutyCancelDialogLogic>().dialogState;

  @override
  void initState() {
    super.initState();
    state.collectId = widget.collectId;
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DutyCancelDialogLogic>(builder: (logic) {
      return ConstrainedBox(
        constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 7.0 / 10.0),
        child: Column(
          children: [
            SingleChildScrollView(
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.centerRight,
                    child: Container(
                      margin: EdgeInsets.only(top: 15.5.r, right: 16.5.r),
                      child: GestureDetector(
                        child: Icon(Icons.close_rounded, color: MyColors.FF999999, size: 25.sp,),
                        onTap: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  ),
                  Text(state.currentMonth + "月份值班表", style: TextStyle(color: MyColors.FF333333, fontSize: 18.sp),),
                  Container(
                    margin: EdgeInsets.only(left: 10.r, right: 10.r),
                    // absorbPointer开启absorbing：本身可以接收点击事件，但不会把事件传递给子组件。
                    // ignorePointer开启ignoring：本身和子组件都不能接收点击事件。
                    child: IgnorePointer(
                      ignoring: true,
                      child: CustomCalendar(
                        key: logic.customCalendarKey,
                        focusedDay: state.focusedDay,
                        firstDay: state.firstDay,
                        lastDay: state.lastDay,
                        disabledDays: state.disabledDays,
                        selectedDays: state.selectedDays,
                        onDateTimeCallback: (List<String> dateTime) {},
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 20.r),
                    child: Row(
                      children: [
                        Image.asset(R.assetsImgIcon56, width: 13.r,),
                        SizedBox(width: 7.r,),
                        // Text("管理值班", style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp, fontWeight: FontWeight.w600),)
                        Text("值班日期", style: TextStyle(color: MyColors.FF333333, fontSize: 15.sp, fontWeight: FontWeight.w600),)
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 5.5.r),
                    child: ListView.builder(
                      shrinkWrap: true,
                      //解决无线高度问题 解决SingleChildScrollView 和listview冲突问题 ListView.builder
                      physics: NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.only(top: 0),
                      itemCount: dialogState.listSignRecordEntity?.length ?? 0,
                      itemBuilder: (BuildContext context, int index) {
                        return OnDutyDetailItem(dialogState.listSignRecordEntity![index]);
                      },
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    });
  }

  // 值班弹窗 值班管理 item
  // status 0已取消1值班中（可申请取消）1已值班
  Widget OnDutyDetailItem(ListSignRecordEntity item){
    return Container(
      margin: EdgeInsets.only(bottom: 8.r, top: 8.r),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Row(children: [
              Container(
                margin: EdgeInsets.only(left:4.r, right: 10.5.r),
                width: 6.r,
                height: 6.r,
                decoration: BoxDecoration(
                    color: MyColors.FFDDDDDD,
                    shape: BoxShape.circle
                ),
              ),
              Text(item.workDate! + "  " + item.week!, style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp),),
            ],),
          ),
          Visibility(
            visible: false,
            child: Container(
              height: 26.r,
              child: OutlinedButton(
                style: ButtonStyle(
                    padding: MaterialStateProperty.all(EdgeInsets.only(left: 10.r, right: 10.r)),
                    side: MaterialStateProperty.all(BorderSide(
                      width: 0.5.r,
                      color: item.status == "1" ? MyColors.FF2763B9 : MyColors.FF999999,
                    ))
                ),
                child: Text(
                  item.status == "0" ? "  已取消  " : item.status == "1" ? "取消申请" : "  已值班  ",
                  style: TextStyle(
                      color: item.status == "1" ? MyColors.FF2763B9 : MyColors.FF999999, fontSize: 13.sp
                  ),
                ),
                onPressed: () {
                  if (item.status == "1") {
                    showDialog(context: context, builder: (context){
                      return Dialog(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(4.r),
                        ),
                        child: CustomCancelDialog(item.workDate!),
                      );
                    });
                  }
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  // 申请 输入框 取消 dialog
  Widget CustomCancelDialog(String workDate){
    return IntrinsicHeight(
      child: Column(
        children: [
          Container(
              margin: EdgeInsets.only(top: 21.5.r),
              child: Text('申请取消原因', style: TextStyle(color: MyColors.FF333333, fontSize: 16.sp, fontWeight: FontWeight.w600),)),
          Container(
            margin: EdgeInsets.all(16.r),
            child: TextField(
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: '请填写申请取消值班的原因',
                hintStyle: TextStyle(color: MyColors.FF999999, fontSize: 14.sp),
                filled: true,
                fillColor: MyColors.FFF1F1F1,
              ),
              style: TextStyle(color: MyColors.FF010101, fontSize: 14.sp, fontWeight: FontWeight.w500),
              // 光标颜色
              cursorColor: MyColors.FF010101,
              // 光标宽度
              cursorWidth: 1,
              minLines: 5,
              maxLines: 5,
              controller: logic.cancelReason,
            ),
          ),
          Divider(thickness: 0.5.r, height: 0, color: MyColors.FFDDDDDD,),
          Container(
            height: 40.r,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  width: 140.r,
                  child: TextButton(
                    child: Text("取消", style: TextStyle(color: MyColors.FF333333, fontSize: 14.sp, fontWeight: FontWeight.w600), ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
                VerticalDivider(
                  width: 0.5.r,
                  color: MyColors.FFDDDDDD,
                ),
                Container(
                  width: 140.r,
                  child: TextButton(
                    child: Text("确认", style: TextStyle(color: MyColors.FF2763B9, fontSize: 14.sp, fontWeight: FontWeight.w600),),
                    onPressed: () {
                      logic.postCancelScheduleSign(workDate);
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<DutyCancelDialogLogic>();
    super.dispose();
  }
}
